function hideDiv(div_id) {
	$("#" + div_id).animate({
		opacity : "hide"
	}, 300);
}
function popupDiv(div_id) {
	var div_obj = $("#" + div_id);
	//窗口宽度,高度
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	//弹出的div的宽度,高度
	var popHeight = div_obj.height();
	var popWidth = div_obj.width();
	div_obj.animate({
		opacity : "show",
		left : (winWidth - popWidth) / 2,
		top : (winHeight - popHeight) / 2,
		width : popWidth,
		height : popHeight
	}, 300);
}
$(function() {
	$("#AddTrain_div").easydrag();
	$("#AddTrain_div").setHandler("#AddTrain_div #AddTrain_head");
});

$(function() {
	$("#UpdateTrain_div").easydrag();
	$("#UpdateTrain_div").setHandler("#UpdateTrain_div #UpdateTrain_head");
});


function popadd()
{
	if(!RolePowerManager())
	{
		return;
	}
	popupDiv('AddTrain_div');
}





var teacherop = "";
var courseop = "";

$(function() {
	TrainTypeManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#traintype").append(
					"<option value='" + data[i].id + "'>" + data[i].typename
							+ "</option>");
		}
	});
	TrainPropertyManager.getAllTrainProperty(function(data) {

		for ( var i = 0; i < data.length; i++) {
			$("#trainProperty").append(
					"<option value='" + data[i].id + "'>"
							+ data[i].propertyname + "</option>");
		}
	});
	TrainWayManager.getAll(function(data) {

		for ( var i = 0; i < data.length; i++) {
			$("#trainWay").append(
					"<option value='" + data[i].id + "'>" + data[i].wayname
							+ "</option>");
		}
	});
	CourseManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#courses").append(
					"<option value='" + data[i].id + "'>" + data[i].name
							+ "</option>");
			courseop = courseop + "<option value='" + data[i].id + "'>"
					+ data[i].name + "</option>";
		}
	});
	TeacherManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#teacher").append(
					"<option value='" + data[i].id + "'>" + data[i].name
							+ "</option>");
			teacherop = teacherop + "<option value='" + data[i].id + "'>"
					+ data[i].name + "</option>";
		}
	});
});

function addCourseInput() {
	$("[class=course]:last")
			.after(
					"<tr id='course' class='course'><td>科目</td>"
							+ "<td>"
							+ "<select name='course' >"
							+ "<option value="
							+ ">"
							+ "--请选择--</option>"
							+ courseop
							+ "</select>"
							+ " 所占比重<input type='text' name='percent' size='4'> 讲师<select name='teacher' ><option value='0'>--请选择--</option>"
							+ teacherop + "</select>" + "</td></tr>" + "");

	$("#AddTrain_div").height(
			$("#AddTrain_div").height() + $("#courseheight").height() + 6);
}
function deleteCourseInput() {
	$("#[class=course]:last").remove();
	$("#AddTrain_div").height(
			$("#AddTrain_div").height() - $("#courseheight").height() - 6);
}

function addTrain() {
	var flag = false;
	if (checktrainname()) {
		if (checktraintype()) {
			if (checktrainProperty()) {
				if (checktrainWay()) {
					if (checkaddress()) {
						if (checkstarttime()) {
							if (checkendtime()) {
								if (checkendtime()) {
									if (checkmoney()) {
										if (checkorganization()) {
											if (checkmanager()) {
												flag = true;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if (!flag) {
		alert("请保持信息完整");
		return;
	}
	var per = 0;
	var perobj = document.getElementsByName("percent");
	for ( var j = 0; j < perobj.length; j++) {
		per = per + parseInt(perobj[j].value);
	}
	if (per != 100) {
		alert("科目比重和未100%");
		return;
	}
	var course = document.getElementsByName("course");
	for ( var i = 0; i < course.length; i++) {
		if (!perobj[i].value) {
			alert("科目名必选");
			return;
		}
	}
	var teacher = document.getElementsByName("teacher");
	for ( var i = 0; i < teacher.length; i++) {
		if (!teacher[i].value) {
			alert("教师必选");
			return;
		}
	}
	$("#addtrainform").submit();
}

function checktrainname() {
	$("#trainnameerr").html("");
	if (!$("#trainname").val()) {
		$("#trainnameerr").html("培训名不能为空");
		return false;
	}
	return true;
}
function checktraintype() {
	$("#traintypeerr").html("");
	if ($("#traintype").val() == 0) {
		$("#traintypeerr").html("培训类型必选");
		return false;
	}
	return true;
}

function checktrainProperty() {
	$("#trainPropertyerr").html("");
	if ($("#trainProperty").val() == 0) {
		$("#trainPropertyerr").html("培训性质必选");
		return false;
	}
	return true;
}

function checktrainWay() {
	$("#trainWayerr").html("");
	if ($("#trainWay").val() == 0) {
		$("#trainWayerr").html("培训方法必选");
		return false;
	}
	return true;
}
function checkaddress() {
	$("#addresserr").html("");
	if (!$("#address").val()) {
		$("#addresserr").html("地址不为空");
		return false;
	}
	return true;
}

function checkstarttime() {
	$("#starttimeerr").html("");
	if (!$("#starttime").val()) {
		$("#starttimeerr").html("开始时间不为空");
		return false;
	}
	return true;
}

function checkendtime() {
	$("#endtimeerr").html("");
	if (!$("#endtime").val()) {
		$("#endtimeerr").html("结束时间不为空");
		return false;
	}
	return true;
}

function checkmoney() {

	$("#moneyerr").html("");
	if (isNaN($("#money").val()) || !$("#money").val()) {
		$("#moneyerr").html("培训费用必须为数字");
		return false;
	}
	return true;
}

function checkorganization() {
	$("#organizationerr").html("");
	if (!$("#organization").val()) {
		$("#organizationerr").html("培训机构不为空");
		return false;
	}
	return true;
}
function checkmanager() {
	$("#managererr").html("");
	if (!$("#manager").val()) {
		$("#managererr").html("负责人不为空");
		return false;
	} else {
		dwrTrainManager.ifEmpNoExist($("#manager").val(), function(data) {
			if (!data) {
				$("#managererr").html("不存在该编号的负责人");
				return false;
			}
		});
	}
	return true;
}










function upchecktrainname() {
	$("#uptrainnameerr").html("");
	if (!$("#uptrainname").val()) {
		$("#uptrainnameerr").html("培训名不能为空");
		return false;
	}
	return true;
}
function upchecktraintype() {
	$("#uptraintypeerr").html("");
	if ($("#uptraintype").val() == 0) {
		$("#uptraintypeerr").html("培训类型必选");
		return false;
	}
	return true;
}

function upchecktrainProperty() {
	$("#uptrainPropertyerr").html("");
	if ($("#uptrainProperty").val() == 0) {
		$("#uptrainPropertyerr").html("培训性质必选");
		return false;
	}
	return true;
}

function upchecktrainWay() {
	$("#uptrainWayerr").html("");
	if ($("#uptrainWay").val() == 0) {
		$("#uptrainWayerr").html("培训方法必选");
		return false;
	}
	return true;
}
function upcheckaddress() {
	$("#upaddresserr").html("");
	if (!$("#upaddress").val()) {
		$("#upaddresserr").html("地址不为空");
		return false;
	}
	return true;
}

function upcheckstarttime() {
	$("#upstarttimeerr").html("");
	if (!$("#upstarttime").val()) {
		$("#upstarttimeerr").html("开始时间不为空");
		return false;
	}
	return true;
}

function upcheckendtime() {
	$("#upendtimeerr").html("");
	if (!$("#upendtime").val()) {
		$("#upendtimeerr").html("结束时间不为空");
		return false;
	}
	return true;
}

function upcheckmoney() {

	$("#upmoneyerr").html("");
	if (isNaN($("#upmoney").val()) || !$("#upmoney").val()) {
		$("#upmoneyerr").html("培训费用必须为数字");
		return false;
	}
	return true;
}

function upcheckorganization() {
	$("#uporganizationerr").html("");
	if (!$("#uporganization").val()) {
		$("#uporganizationerr").html("培训机构不为空");
		return false;
	}
	return true;
}




//点击详情/修改后触发的事件
function clickUpdateTrain(id)
{
	popupDiv("UpdateTrain_div");
	
	TrainTypeManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#uptraintype").append(
					"<option value='" + data[i].id + "'>" + data[i].typename
							+ "</option>");
		}
	});
	TrainPropertyManager.getAllTrainProperty(function(data) {

		for ( var i = 0; i < data.length; i++) {
			$("#uptrainProperty").append(
					"<option value='" + data[i].id + "'>"
							+ data[i].propertyname + "</option>");
		}
	});
	TrainWayManager.getAll(function(data) {

		for ( var i = 0; i < data.length; i++) {
			$("#uptrainWay").append(
					"<option value='" + data[i].id + "'>" + data[i].wayname
							+ "</option>");
		}
	});
	CourseManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#upcourses").append(
					"<option value='" + data[i].id + "'>" + data[i].name
							+ "</option>");
			courseop = courseop + "<option value='" + data[i].id + "'>"
					+ data[i].name + "</option>";
		}
	});
	
	//赋值
	dwrTrainManager.getById(id,function(data)
			{
				$("#uptrainid").val(data.id);
				$("#uptrainname").val(data.trainname);
				$("#uptraintype").val(data.trainType.id);
				$("#uptrainProperty").val(data.trainProperty.id);
				$("#uptrainWay").val(data.trainWay.id);
				$("#upaddress").val(data.address);
				$("#upstarttime").val(data.starttime);
				$("#upendtime").val(data.endtime);
				$("#upmoney").val(data.money);
				$("#uporganization").val(data.organization);
				$("#upmanager").html(data.manager.name);
				
				for(var i=0;i<data.trainCourse.length;i++)
				{	
					var html="<tr id='upcourseheight'><td>科目</td><td>"+data.trainCourse[i].course.name+"===讲师:"+data.trainCourse[i].teacher.name+
					" ===比重："+data.trainCourse[i].percent+"</td></tr>";
					$("[class=upcourse]:last").after(html);
					
					$("#UpdateTrain_div").height($("#UpdateTrain_div").height() + $("#upcourseheight").height() + 6);
				}
			});
	
			
}
//关闭窗口顺带刷行，避免多行数据的问题出现
function closediv()
{
	hideDiv('UpdateTrain_div');
	location.reload();
}
//更新
function updateTrain()
{
	if(!RolePowerManager())
	{
		return;
	}
	var flag = false;
	if (upchecktrainname()) {
		if (upchecktraintype()) {
			if (upchecktrainProperty()) {
				if (upchecktrainWay()) {
					if (upcheckaddress()) {
						if (upcheckstarttime()) {
							if (upcheckendtime()) {
								if (upcheckendtime()) {
									if (upcheckmoney()) {
										if (upcheckorganization()) {
											
												flag = true;
											
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if (!flag) {
		alert("请保持信息完整");
		return;
	}
	$("#updatetrainform").submit();
}

//更换每页显示页数
function changePageSize()
{
		var pageSize=$("#pageSize").val();
		location.href="FindTrainServlet?pageSize="+pageSize;
}

function changesearchtype()
{
	
	
	var type=$("#serchtype").val();
	//对css进行控制
	if("time"==type)
		{
			$("#searchtypeoption").animate({opacity : "hide"}, 500);
			$("#searchtinput").animate({opacity : "hide"}, 500);
		}
	else if("trainname"==type)
		{		
		$("#searchtypeoption").animate({opacity : "hide"}, 500);
		$("#searchtinput").animate({opacity : "show"}, 500);
		}else
		{
			$("#searchtypeoption").animate({opacity : "show"}, 500);
			$("#searchtinput").animate({opacity : "hide"}, 500);	
		}
	//如果是按类型查询
	if("trainType"==type)
		{
		$("#idkeycode").empty();
			TrainTypeManager.getAll(function(data)
					{
						for(var i=0;i<data.length;i++)
							{
								$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].typename+"</option>");
							}
					});
		}
	if("trainWay"==type)
	{
	$("#idkeycode").empty();
	TrainWayManager.getAll(function(data)
				{
					for(var i=0;i<data.length;i++)
						{
							$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].wayname+"</option>");
						}
				});
	}
	if("trainProperty"==type)
	{
	$("#idkeycode").empty();
	TrainPropertyManager.getAllTrainProperty(function(data)
				{
					for(var i=0;i<data.length;i++)
						{
							$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].propertyname+"</option>");
						}
				});
	}
}

function search()
{
	if($("#searchstarttime").val()||$("#searchendtime").val())
	{
		$("#serchtype").val("time");
		$("#searchform").submit();
	}else
		{
			$("#searchform").submit();
		}
	
}
function deleteTrain(id)
{
	if(!RolePowerManager())
	{
		return;
	}
	if(!confirm("是否确定删除 ？"))
	{
		return;
	}
	dwrTrainManager.deleteById(id,function(data)
			{
				if(data)
					{
					alert("删除成功");
					location.href="FindTrainServlet";
					}else
						{
						alert("删除失败");
						}
			});
}